package cup.com.icss.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.model.User;
import cup.com.icss.service.UserService;


@Controller
public class loginController {
	
	@Resource
	private UserService userService;
	
	@RequestMapping(value="/login",method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView login(HttpServletRequest request,User user){
		ModelAndView view = new ModelAndView();
		try {
			User userSession = userService.loginValidation(user);
			if(userSession!=null && userSession.getDisable().equals("1")){
				view.addObject("result","200");
				view.addObject("user",userSession);
				view.addObject("roleName",userSession.getRoleName());
				view.addObject("studentId",userSession.getStudentId());
				request.getSession().setAttribute("USER", userSession);
				view.setViewName("cup/admin");
			}else if(userSession!=null && userSession.getDisable().equals("0")){
				view.addObject("result","400");
			}else{
				view.addObject("result","500");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			view.addObject("result","600");
			e.printStackTrace();
		}
		return view;
	}
	
	@RequestMapping(value="/outSession",method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView outSession(HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = new ModelAndView();
		HttpSession session=request.getSession();
		session.invalidate();
		view.setViewName("login");
		return view;
	}
	
	
	@RequestMapping(value="/homePage",method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView home(HttpServletRequest request,HttpServletResponse response,String storiedBuilding){
		ModelAndView view = new ModelAndView();
		
		User user = (User)request.getSession().getAttribute("USER");
		
		view.addObject("storiedBuilding",storiedBuilding);
		view.addObject("studentId",user.getStudentId());
		view.setViewName("cup/jsp/home");
		return view;
	}
	
	@RequestMapping(value="/changePassWordValidation",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> changePassWordValidation(HttpServletRequest request,User record){
		Map<String, String> map = new HashMap<String,String>();
		User user = userService.loginValidation(record);
		if(user!=null){
			map.put("state", "200");
		}else{
			map.put("state", "500");
		}
		return map;
	}
	
	@RequestMapping(value="/changePassWord",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> changePassWord(HttpServletRequest request,User record){
		Map<String, String> map = new HashMap<String,String>();
		int update = userService.updatePassWord(record);
		if(update>0){
			map.put("state", "200");
		}else{
			map.put("state", "500");
		}
		return map;
	}
}
